import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { useUserInfoStore } from '@/stores/userInfo'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login.vue')
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('@/views/Register.vue')
    },
    {
        path: '/',
        component: () => import('@/views/Layout.vue'),
        children: [
            // 通用路由
            { path: '', redirect: '/home' },
            { path: 'home', name: 'Home', component: () => import('@/views/Home.vue') },
            { path: 'hotel', name: 'Hotel', component: () => import('@/views/hotel/HotelList.vue') },
            { path: 'rooms', name: 'Rooms', component: () => import('@/views/room/PublicRoomList.vue') },
            { path: 'profile', name: 'Profile', component: () => import('@/views/user/UserProfile.vue') },
            { path: 'order', name: 'MyOrders', component: () => import('@/views/order/UserOrderList.vue') },
            { path: 'review', name: 'MyReviews', component: () => import('@/views/review/UserReviewList.vue') },
            { path: 'messages', name: 'UserMessages', component: () => import('@/views/user/UserMessages.vue') },
            
            // 新添加的路由
            { path: 'hotel/:hotelName/rooms', name: 'HotelRooms', component: () => import('@/views/hotel/HotelRoomList.vue') },
            { path: 'hotel/:hotelName/detail', name: 'HotelDetail', component: () => import('@/views/hotel/HotelDetailView.vue') },
            { path: 'create-order', name: 'CreateOrder', component: () => import('@/views/order/CreateOrderPage.vue') },
            
            // 需要实现的新页面
            // 通用功能
            { path: 'checkout', name: 'Checkout', component: () => import('@/views/order/UserCheckoutList.vue') },
            
            // 酒店商家功能
            { path: 'my-hotel', name: 'MyHotel', component: () => import('@/views/hotel/MyHotel.vue') },
            // 酒店商家子导航
            { path: 'my-hotel/detail', name: 'MyHotelDetail', component: () => import('@/views/hotel/HotelDetail.vue') },
            { path: 'my-hotel/rooms', name: 'MyHotelRooms', component: () => import('@/views/hotel/HotelRoomManage.vue') },
            { path: 'my-hotel/orders', name: 'MyHotelOrders', component: () => import('@/views/hotel/HotelOrderList.vue') },
            { path: 'my-hotel/reviews', name: 'MyHotelReviews', component: () => import('@/views/hotel/HotelReviewList.vue') },
            
            // 管理员功能
            { path: 'user', name: 'UserManage', component: () => import('@/views/user/UserList.vue') },
            { path: 'admin-hotel', name: 'HotelManage', component: () => import('@/views/hotel/AdminHotelList.vue') },
            { path: 'room', name: 'RoomManage', component: () => import('@/views/room/RoomList.vue') },
            { path: 'admin-order', name: 'OrderManage', component: () => import('@/views/order/OrderList.vue') },
            { path: 'checkout-records', name: 'HistoryOrders', component: () => import('@/views/order/HistoryOrderList.vue') },
            { path: 'admin-review', name: 'ReviewManage', component: () => import('@/views/review/ReviewList.vue') }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const userInfoStore = useUserInfoStore()
    
    // 允许直接访问登录页和注册页
    if (to.path === '/login' || to.path === '/register') {
        next()
        return
    }
    
    // 未登录跳转到登录页
    if (!userInfoStore.userInfo) {
        next('/login')
        return
    }
    
    // 权限路由控制
    const isAdmin = userInfoStore.isAdmin()
    const isHotelOwner = userInfoStore.isHotelOwner()
    
    // 管理员专属路由
    const adminRoutes = ['/user', '/admin-hotel', '/admin-order', '/checkout-records', '/admin-review']
    // 酒店商家路由
    const hotelOwnerRoutes = ['/my-hotel', '/my-hotel/detail', '/my-hotel/rooms', '/my-hotel/orders', '/my-hotel/reviews']
    
    if (adminRoutes.includes(to.path) && !isAdmin) {
        // 非管理员访问管理员路由，重定向到首页
        next('/hotel')
        return
    }
    
    if (hotelOwnerRoutes.includes(to.path) && !isHotelOwner) {
        // 非酒店商家访问酒店商家路由，重定向到首页
        next('/hotel')
        return
    }
    
    next()
})

export default router
